package com.player.thirdGroup.controller;
import com.player.thirdGroup.entity.Dept;
import com.player.thirdGroup.entity.Message;
import com.player.thirdGroup.entity.Player;
import com.player.thirdGroup.entity.User;
import com.player.thirdGroup.service.DeptService;
import com.player.thirdGroup.util.IdWorker;
import com.player.thirdGroup.util.LoadUtil;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author VULCAN
 * @create 2019-12-28 7:49
 * @desc 单位
 **/
@Controller
public class DeptController {
    @Autowired
    private DeptService service;
    @Autowired
    private IdWorker idWorker;
    //列表
    @GetMapping("deptlist")
    public String getDept(Model model){
        List<Dept> all = service.findAll();
        model.addAttribute("list",all);
        System.err.println(all+"");
        return "/index";
    }
    /**
     * 创建单位中转
     * @return
     */
    @GetMapping("add")
    public String add(){
        return "deptadd";
    }
    /**
     * 创建单位
     * @param
     */
    @RequestMapping(value = "deptAdd",method = RequestMethod.POST)
    public String deptAdd(MultipartFile img,
                          String name,
                          String person,
                          String phone,
                          String status){
        Dept dept = new Dept();
        dept.setId(idWorker.nextId()+"");
        dept.setName(name);
        dept.setPerson(person);
        dept.setPhone(phone);
        dept.setCdate(new Date());
        dept.setStatus(status);
        String dimg ="/img/"+img.getOriginalFilename();
        String s = LoadUtil.ScPhoto(img);
        dept.setImg(dimg);
        service.deptAdd(dept);
        return "examine";
    }
    //列表
    @GetMapping("deptunderlist")
    @ResponseBody
    public List<Dept> deptunderlist(){
        List<Dept> all = service.findAll();
        return all;
    }
    //列表 中转
    @GetMapping("deptunder")
    public String deptinder(Model model){
        String countpass =  service.countpass();
        String countnopass = service.countnopass();
        String countunder = service.countunder();
        model.addAttribute("countunder",countunder);
        model.addAttribute("countnopass",countnopass);
        model.addAttribute("countpass",countpass);
        return "deptunder";
    }
    //审核通过
    @GetMapping("deptunderpass")
    @ResponseBody
    public Dept deptunderpass(String id) throws UnknownHostException {
         return service.deptunderpass(id);
    }
    //审核不通过
    @GetMapping("deptundernopass")
    @ResponseBody
    public Dept deptundernopass(String id) throws UnknownHostException {
        return service.deptundernopass(id);
    }
    //批量审核通过
    @RequestMapping(value = "bulkpass",method = RequestMethod.GET)
    @ResponseBody
    public String bulkpass(@RequestParam Long[] ids) throws UnknownHostException {
         return service.bulkpass(ids);
    }
    //批量审核不通过
    @RequestMapping(value = "bulknopass",method = RequestMethod.GET)
    @ResponseBody
    public String bulknopass(@RequestParam Long[] ids) throws UnknownHostException {
        return service.bulknopass(ids);
    }
    //改变状态 是否是 审核中 审核通过  审核不通过 数据
    @RequestMapping(value = "updatestatus",method = RequestMethod.GET)
    @ResponseBody
    public List<Dept> updatestatus(String status){
        return service.updatestatus(status);
    }
    //驳回
    @RequestMapping(value = "deptfiledown",method = RequestMethod.POST)
    @ResponseBody
    public String deptreason(String id , User user) {
        service.deptreason(id,user);
        return "驳回成功";
    }
    //批量驳回
    @RequestMapping(value = "deptfiledownids",method = RequestMethod.POST)
    @ResponseBody
    public String deptfiledownids(@RequestParam Long[] ids , User user) {
        service.deptreasonids(ids,user);
        return "驳回成功";
    }
    //查看驳回原因
    @RequestMapping(value = "deptlookturn",method = RequestMethod.GET)
    @ResponseBody
    public Message lookturn(String id) {

        return service.lookturn(id);
    }
    //单位管理列表中转
    @GetMapping("Administration")
    public String administrationlist(){
        return "Administrationlist";
    }
    //单位管理列表
    @RequestMapping("Administrationlist")
    @ResponseBody
    public List<Dept> administrationList(){
        List<Dept> guan= service.administrationlist();
        return guan;
    }
    //账号停用
    @RequestMapping("accountDelete")
    @ResponseBody
    public String  accountDelete(String id){
        service.accountDelete(id);
        return "yes";
    }
    //导出单位数据
    @RequestMapping("listOutPutExcel")
    @ResponseBody
    public void export2(HttpServletRequest req, HttpServletResponse resp, String type) throws IOException {
        //列名
        List<String> headers = Arrays.asList("ID", "单位名称", "联系人", "注册电话", "单位资质" , "时间");
        List<Map<String, String>> exportDatas = new ArrayList<Map<String, String>>();
        List<Dept> orderProductAttrList = service.findAll();

        //将查询结果，存放进列值
        for (Dept dept : orderProductAttrList) {
            Map<String,String> map=new HashMap<>();
            map.put("ID", dept.getId());
            map.put("单位名称", dept.getName());
            map.put("联系人", dept.getPerson());
            map.put("注册电话", dept.getPhone());
            map.put("单位资质", dept.getImg());
            map.put("时间", dept.getCdate()+"");
            exportDatas.add(map);
        }
        //导出
        exportExcel(req,resp,headers,exportDatas);
    }

    public void exportExcel(HttpServletRequest req, HttpServletResponse resp, List<String> headers, List<Map<String, String>> exportDatas) throws IOException {
        // 创建一个工作薄
        SXSSFWorkbook wb = new SXSSFWorkbook();
        //创建sheet
        Sheet sh = wb.createSheet("单位信息");
        //设置表头字体
        Font headFont = wb.createFont();
        headFont.setFontName("宋体");
        headFont.setColor(HSSFColor.WHITE.index);
        headFont.setFontHeightInPoints((short) 10);// 字体大小
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗

        // 设置表头样式
        CellStyle headStyle = wb.createCellStyle();
        headStyle.setFont(headFont);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
        headStyle.setLocked(true);
        headStyle.setWrapText(true);// 自动换行
        headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headStyle.setFillForegroundColor(HSSFColor.GREEN.index);

        // 设置普通单元格字体
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 9);

        // 设置普通单元格样式
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
        style.setWrapText(true);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft((short) 1);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight((short) 1);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
        style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色．
        style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色．

        //设置单位格样式为文本
        DataFormat dataFormat = wb.createDataFormat();
        style.setDataFormat(dataFormat.getFormat("@"));

        /**
         * 设置列名
         */
        Row sheetRow = sh.createRow(0);
        Cell cell = null;
        for (int i = 0; i < headers.size(); i++) {
            cell = sheetRow.createCell(i);
            cell.setCellValue(headers.get(i));
            cell.setCellStyle(headStyle);
        }

        /**
         * 设置列值
         */
        int rows = 1;
        for (Map<String, String> data : exportDatas) {
            Row row = sh.createRow(rows++);
            int initCellNo = 0;
            int titleSize = headers.size();
            for (int i = 0; i < titleSize; i++) {
                String key = headers.get(i);
                Object object = data.get(key);
                if (object == null) {
                    row.createCell(initCellNo).setCellValue("");
                } else {
                    row.createCell(initCellNo).setCellValue(String.valueOf(object));
                }
                initCellNo++;
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "单位数据" + sdf.format(new Date()) + ".xlsx";
        resp.setContentType("application/octet-stream;charset=utf-8");
        resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        ServletOutputStream out = resp.getOutputStream();
        wb.write(out);
        out.close();
    }

    /**
     * 导出所有单位名单
     * @param req
     * @param resp
     * @param type
     * @throws IOException
     */
    @RequestMapping("daoExlList")
    @ResponseBody
    public void daoExl(HttpServletRequest req, HttpServletResponse resp, String type) throws IOException {
        //列名
        List<String> headers = Arrays.asList("ID", "单位名称", "联系人");
        List<Map<String, String>> exportDatas = new ArrayList<Map<String, String>>();
        List<Dept> orderProductAttrList = service.findAll();

        //将查询结果，存放进列值
        for (Dept dept : orderProductAttrList) {
            Map<String,String> map=new HashMap<>();
            map.put("ID", dept.getId());
            map.put("单位名称", dept.getName());
            map.put("联系人",dept.getPerson());
            exportDatas.add(map);
        }
        //导出
        exportExcel(req,resp,headers,exportDatas);
    }

    public void exportExcel1(HttpServletRequest req, HttpServletResponse resp, List<String> headers, List<Map<String, String>> exportDatas) throws IOException {
        // 创建一个工作薄
        SXSSFWorkbook wb = new SXSSFWorkbook();
        //创建sheet
        Sheet sh = wb.createSheet("单位信息");
        //设置表头字体
        Font headFont = wb.createFont();
        headFont.setFontName("宋体");
        headFont.setColor(HSSFColor.WHITE.index);
        headFont.setFontHeightInPoints((short) 10);// 字体大小
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗

        // 设置表头样式
        CellStyle headStyle = wb.createCellStyle();
        headStyle.setFont(headFont);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
        headStyle.setLocked(true);
        headStyle.setWrapText(true);// 自动换行
        headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headStyle.setFillForegroundColor(HSSFColor.GREEN.index);

        // 设置普通单元格字体
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 9);

        // 设置普通单元格样式
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
        style.setWrapText(true);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft((short) 1);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight((short) 1);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
        style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色．
        style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色．

        //设置单位格样式为文本
        DataFormat dataFormat = wb.createDataFormat();
        style.setDataFormat(dataFormat.getFormat("@"));

        /**
         * 设置列名
         */
        Row sheetRow = sh.createRow(0);
        Cell cell = null;
        for (int i = 0; i < headers.size(); i++) {
            cell = sheetRow.createCell(i);
            cell.setCellValue(headers.get(i));
            cell.setCellStyle(headStyle);
        }

        /**
         * 设置列值
         */
        int rows = 1;
        for (Map<String, String> data : exportDatas) {
            Row row = sh.createRow(rows++);
            int initCellNo = 0;
            int titleSize = headers.size();
            for (int i = 0; i < titleSize; i++) {
                String key = headers.get(i);
                Object object = data.get(key);
                if (object == null) {
                    row.createCell(initCellNo).setCellValue("");
                } else {
                    row.createCell(initCellNo).setCellValue(String.valueOf(object));
                }
                initCellNo++;
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "所有单位信息" + sdf.format(new Date()) + ".xlsx";
        resp.setContentType("application/octet-stream;charset=utf-8");
        resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        ServletOutputStream out = resp.getOutputStream();
        wb.write(out);
        out.close();
    }

    @RequestMapping("daoRenExlList")
    @ResponseBody
    public void daoRenExlList(HttpServletRequest req, HttpServletResponse resp, String type) throws IOException {
        //列名
        List<String> headers = Arrays.asList("ID", "单位名称");
        List<Map<String, String>> exportDatas = new ArrayList<Map<String, String>>();
        List<Dept> orderProductAttrList = service.findAll();

        //将查询结果，存放进列值
        for (Dept dept : orderProductAttrList) {
            Map<String,String> map=new HashMap<>();
            map.put("ID", dept.getId());
            map.put("单位名称", dept.getName());
            exportDatas.add(map);
        }
        //导出
        exportExcel(req,resp,headers,exportDatas);
    }

    public void daoRenExlList1(HttpServletRequest req, HttpServletResponse resp, List<String> headers, List<Map<String, String>> exportDatas) throws IOException {
        // 创建一个工作薄
        SXSSFWorkbook wb = new SXSSFWorkbook();
        //创建sheet
        Sheet sh = wb.createSheet("单位信息");
        //设置表头字体
        Font headFont = wb.createFont();
        headFont.setFontName("宋体");
        headFont.setColor(HSSFColor.WHITE.index);
        headFont.setFontHeightInPoints((short) 10);// 字体大小
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗

        // 设置表头样式
        CellStyle headStyle = wb.createCellStyle();
        headStyle.setFont(headFont);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
        headStyle.setLocked(true);
        headStyle.setWrapText(true);// 自动换行
        headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headStyle.setFillForegroundColor(HSSFColor.GREEN.index);

        // 设置普通单元格字体
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 9);

        // 设置普通单元格样式
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
        style.setWrapText(true);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft((short) 1);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight((short) 1);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
        style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色．
        style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色．

        //设置单位格样式为文本
        DataFormat dataFormat = wb.createDataFormat();
        style.setDataFormat(dataFormat.getFormat("@"));

        /**
         * 设置列名
         */
        Row sheetRow = sh.createRow(0);
        Cell cell = null;
        for (int i = 0; i < headers.size(); i++) {
            cell = sheetRow.createCell(i);
            cell.setCellValue(headers.get(i));
            cell.setCellStyle(headStyle);
        }

        /**
         * 设置列值
         */
        int rows = 1;
        for (Map<String, String> data : exportDatas) {
            Row row = sh.createRow(rows++);
            int initCellNo = 0;
            int titleSize = headers.size();
            for (int i = 0; i < titleSize; i++) {
                String key = headers.get(i);
                Object object = data.get(key);
                if (object == null) {
                    row.createCell(initCellNo).setCellValue("");
                } else {
                    row.createCell(initCellNo).setCellValue(String.valueOf(object));
                }
                initCellNo++;
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "单位所以人员信息" + sdf.format(new Date()) + ".xlsx";
        resp.setContentType("application/octet-stream;charset=utf-8");
        resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        ServletOutputStream out = resp.getOutputStream();
        wb.write(out);
        out.close();
    }
    //查询人员信息
    @RequestMapping(value = "lookplayer",method = RequestMethod.GET)
    @ResponseBody
    public List<Player>  lookplayer(String id){
        return service.lookplayer(id);
    }

    //导出人员
    @RequestMapping("daochuModelList")
    @ResponseBody
    public void daochuModelList(HttpServletRequest req, HttpServletResponse resp, String type) throws IOException {
        //列名
        List<String> headers = Arrays.asList("ID", "单位名称");
        List<Map<String, String>> exportDatas = new ArrayList<Map<String, String>>();
        List<Dept> orderProductAttrList = service.findAll();

        //将查询结果，存放进列值
        for (Dept dept : orderProductAttrList) {
            Map<String,String> map=new HashMap<>();
            map.put("ID", dept.getId());
            map.put("单位名称", dept.getName());
            exportDatas.add(map);
        }
        //导出
        exportExcel(req,resp,headers,exportDatas);
    }

    public void daochuModelList1(HttpServletRequest req, HttpServletResponse resp, List<String> headers, List<Map<String, String>> exportDatas) throws IOException {
        // 创建一个工作薄
        SXSSFWorkbook wb = new SXSSFWorkbook();
        //创建sheet
        Sheet sh = wb.createSheet("单位信息");
        //设置表头字体
        Font headFont = wb.createFont();
        headFont.setFontName("宋体");
        headFont.setColor(HSSFColor.WHITE.index);
        headFont.setFontHeightInPoints((short) 10);// 字体大小
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗

        // 设置表头样式
        CellStyle headStyle = wb.createCellStyle();
        headStyle.setFont(headFont);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
        headStyle.setLocked(true);
        headStyle.setWrapText(true);// 自动换行
        headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headStyle.setFillForegroundColor(HSSFColor.GREEN.index);

        // 设置普通单元格字体
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 9);

        // 设置普通单元格样式
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
        style.setWrapText(true);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft((short) 1);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight((short) 1);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
        style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色．
        style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色．

        //设置单位格样式为文本
        DataFormat dataFormat = wb.createDataFormat();
        style.setDataFormat(dataFormat.getFormat("@"));

        /**
         * 设置列名
         */
        Row sheetRow = sh.createRow(0);
        Cell cell = null;
        for (int i = 0; i < headers.size(); i++) {
            cell = sheetRow.createCell(i);
            cell.setCellValue(headers.get(i));
            cell.setCellStyle(headStyle);
        }

        /**
         * 设置列值
         */
        int rows = 1;
        for (Map<String, String> data : exportDatas) {
            Row row = sh.createRow(rows++);
            int initCellNo = 0;
            int titleSize = headers.size();
            for (int i = 0; i < titleSize; i++) {
                String key = headers.get(i);
                Object object = data.get(key);
                if (object == null) {
                    row.createCell(initCellNo).setCellValue("");
                } else {
                    row.createCell(initCellNo).setCellValue(String.valueOf(object));
                }
                initCellNo++;
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "单位所以人员信息" + sdf.format(new Date()) + ".xlsx";
        resp.setContentType("application/octet-stream;charset=utf-8");
        resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        ServletOutputStream out = resp.getOutputStream();
        wb.write(out);
        out.close();
    }
}
